﻿
using DAL3;
using LabelManager2;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using static GEMINI_OP130.FormMain;

namespace GEMINI_OP130
{
    public partial class FormManMode : Form
    {
        private string neirong;
        public string Supplement0;      //用于序列号补0
        public bool bu0;            //用于序列号单次补0
        public FormManMode()
        {
            InitializeComponent();
        }
        //静音
        private void button2_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX0.2", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.2"));
        }
        //工位屏蔽
        private void button3_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX0.3", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.3"));
        }
        //RFID屏蔽
        private void button4_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX0.4", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.4"));
        }
        //相机屏蔽
        private void button5_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX0.5", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.5"));
        }
        //扫码屏蔽
        private void button6_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX0.6", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.6"));
        }
        //拧紧枪屏蔽1
        private void button7_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX0.7", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.7"));
        }
        //拧紧枪屏蔽2
        private void button8_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.0", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.0"));
        }
        //返回原位（大复位）
        private void button9_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.1", true);
        }
        private void button9_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.1", false);
        }
        //报警复位
        private void button10_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.2", true);
        }

        private void button10_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.2", false);
        }
        //防螺钉掉入屏蔽
        private void button11_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX2.5", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX2.5"));
        }
        //返修上线
        private void button12_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.4", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.4"));
        }
        //强制不合格
        private void button13_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.5", true);
        }

        private void button13_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.5", false);
        }
        //空托盘
        private void button14_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.6", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.6"));
        }
        //放行
        private void button15_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.7", true);
        }

        private void button15_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.7", false);

        }
        
        






        //状态
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.2"))
            {
                button2.BackColor = Color.Green;
            }
            else
            {
                button2.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.3"))
            {
                button3.BackColor = Color.Green;
            }
            else
            {
                button3.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.4"))
            {
                button4.BackColor = Color.Green;
            }
            else
            {
                button4.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.5"))
            {
                button5.BackColor = Color.Green;
            }
            else
            {
                button5.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.6"))
            {
                button6.BackColor = Color.Green;
            }
            else
            {
                button6.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX0.7"))
            {
                button7.BackColor = Color.Green;
            }
            else
            {
                button7.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.0"))
            {
                button8.BackColor = Color.Green;
            }
            else
            {
                button8.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.1"))
            {
                button9.BackColor = Color.Green;
            }
            else
            {
                button9.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.2"))
            {
                button10.BackColor = Color.Green;
            }
            else
            {
                button10.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.3"))
            {
                button11.BackColor = Color.Green;
            }
            else
            {
                button11.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.4"))
            {
                button12.BackColor = Color.Green;
            }
            else
            {
                button12.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.5"))
            {
                button13.BackColor = Color.Green;
            }
            else
            {
                button13.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.6"))
            {
                button14.BackColor = Color.Green;
            }
            else
            {
                button14.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.7"))
            {
                button15.BackColor = Color.Green;
            }
            else
            {
                button15.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX28.0"))     //顶升气缸伸出到位
            {
                button1.BackColor = Color.Green;
            }
            else
            {
                button1.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX28.1"))
            {
                button16.BackColor = Color.Green;
            }
            else
            {
                button16.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX2.0"))
            {
                button18.BackColor = Color.Green;   //OP90挡停打开
            }
            else
            {
                button18.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX30.1"))
            {
                button19.BackColor = Color.Green;   //NG挡停缩回
            }
            else
            {
                button19.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX2.1"))
            {
                button20.BackColor = Color.Green;   //OP100挡停打开
            }
            else
            {
                button20.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX32.0"))
            {
                button21.BackColor = Color.Green;   //NG移栽伸出
            }
            else
            {
                button21.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX32.1"))
            {
                button22.BackColor = Color.Green;   //NG移栽缩回
            }
            else
            {
                button22.BackColor = Color.Silver;
            }
            if (FormMain.PLC_Link[0].readBoolen("DB10050.DBX30.0"))
            {
                button23.BackColor = Color.Green;   //NG挡停伸出
            }
            else
            {
                button23.BackColor = Color.Silver;
            }
            if (textBox6.Text == "")      //如果textBox6为空时准备进行补0操作
            {
                bu0 = false;
                Supplement0 = "";
            }
            else if(!bu0)
            {
                for (int i = 10000; Convert.ToInt32(textBox6.Text) / i == 0; i = i / 10)
                {
                    Supplement0 = Supplement0 + "0";
                }
                bu0 = true;
            }
            else
            {

            }
        }
        //顶升气缸伸出
        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX4.0", true);
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX4.0", false);
        }
        //顶升气缸缩回
        private void button16_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX4.1", true);
        }

        private void button16_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX4.1", false);
        }
        //测试上线
        private void button17_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX1.3", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX1.3"));
        }
        //OP100前挡停打开（OP90挡停）
        private void button18_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX2.0", true);
        }

        private void button18_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX2.0", false);
        }
        //OP100后挡停打开
        private void button20_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX2.1", true);
        }

        private void button20_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX2.1", false);
        }
        //NG挡停缩回
        private void button19_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX6.1", true);
        }
        private void button19_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX6.1", false);
        }
        //NG移栽伸出
        private void button21_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX8.0", true);
        }
        private void button21_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX8.0", false);
        }
        //NG移栽缩回
        private void button22_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX8.1", true);
        }
        private void button22_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX8.1", false);
        }
        //NG挡停伸出
        private void button23_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX6.0", true);
        }
        private void button23_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX6.0", false);
        }
        ////暖芯手动正压测试
        //private void button21_Click(object sender, EventArgs e)
        //{

        //}
        ////暖芯手动负压测试
        //private void button22_Click(object sender, EventArgs e)
        //{

        //}
        ////蒸发器手动负压测试
        //private void button23_Click(object sender, EventArgs e)
        //{

        //}
        //手动打开真空泵
        private void button25_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX96.7", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX96.7"));
        }
        //手动切换
        private void button28_Click(object sender, EventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX97.0", !FormMain.PLC_Link[0].readBoolen("DB10050.DBX97.0"));
        }
        //正压程序号写入
        private void button26_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX100.0", true);
        }

        private void button26_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX100.0", false);
        }
        //负压程序号写入
        private void button27_MouseDown(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX100.1", true);
        }

        private void button27_MouseUp(object sender, MouseEventArgs e)
        {
            FormMain.PLC_Link[0].WriteBoolen("DB10050.DBX100.1", false);
        }

        private void button24_Click(object sender, EventArgs e)
        {
            ManPrintLab(@"d:\print\" + comboBox2.Text, "OK print", textBox7.Text);
            bu0 = false;
            Supplement0 = "";
        }
        //手动补打标签
        public void ManPrintLab(string labName, string strPrintName, string pagcont)
        {

            ApplicationClass lbl = new ApplicationClass();

            try
            {
                lbl.Documents.Open(labName, false);
                Document doc = lbl.ActiveDocument;
                doc.Printer.SwitchTo(strPrintName, "", true);
                for (int i = 0; i < Convert.ToInt32(pagcont); i++)
                {
                    if (comboBox2.Text.Contains("DIDI"))
                    {
                        neirong = "[>16" + textBox1.Text + "V" + textBox2.Text + "D" + textBox5.Text + textBox3.Text + "S" + textBox4.Text + "N" + "10" + (Convert.ToInt32(textBox6.Text) + i).ToString();
                    }
                    else
                    {
                        neirong =  textBox2.Text + "#" + textBox3.Text + "#" + textBox4.Text + "#" + textBox5.Text + Supplement0 + (Convert.ToInt32(textBox6.Text) + i).ToString();

                    }
                    doc.Variables.FreeVariables.Item("变量0").Value = neirong;
                    doc.Variables.FreeVariables.Item("变量1").Value = textBox1.Text;
                    doc.Variables.FreeVariables.Item("变量2").Value = textBox2.Text;
                    //变量3后为序列，打印出的标签需给序列号补0（补0未完成）
                    doc.Variables.FreeVariables.Item("变量3").Value = textBox5.Text + Supplement0 + (Convert.ToInt32(textBox6.Text) + i).ToString();
                    doc.Variables.FreeVariables.Item("变量4").Value = textBox3.Text;
                    doc.Variables.FreeVariables.Item("变量5").Value = textBox4.Text;
                    doc.Variables.FreeVariables.Item("变量6").Value = Supplement0 + (Convert.ToInt32(textBox6.Text) + i).ToString();
                    doc.PrintDocument(1);
                }
                //printD = false;
                //printDok = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                lbl.Quit();
            }


        }

        private void FormManMode_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = FormMain.typeArray;
            #region 获取打印模板
            List<string> filenames = new List<string>();
            GetFileNames(ref filenames);
            comboBox2.DataSource = filenames;
            #endregion
        }

        public void GetFileNames(ref List<string> FileNames)
        {
            string ext = ".lab";

            DirectoryInfo theFolder = new DirectoryInfo(@"d:\print\");
            foreach (FileInfo file in theFolder.GetFiles())
            {
                if (file.Extension.ToUpper() == ext.ToUpper())
                {
                    FileNames.Add(file.FullName.Substring(file.FullName.LastIndexOf("\\") + 1, file.FullName.Length - 1 - file.FullName.LastIndexOf("\\")));

                }
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            #region 向服务器获取打印内容
            //label28.Text = "向服务器获取打印内容";
            string sql = "select [零件号],[版本号],[AI代码号],[产线班次号],[打印模板名称] from parameterSetting where [typeName] = " + "'" + comboBox1.Text + "'";
            SqlDataReader objReader1 = SQLHelp.GetReader(sql);

            while (objReader1.Read())
            {
                print.lingjianhao = objReader1["零件号"].ToString();
                print.banbenhao = objReader1["版本号"].ToString();
                print.AIdaima = objReader1["AI代码号"].ToString();
                print.chanxianbanci = objReader1["产线班次号"].ToString();
                print.biaoqianmuban = objReader1["打印模板名称"].ToString();
            }
            objReader1.Close();
            #endregion
            //手动标签内容填充
            textBox1.Text = print.lingjianhao;
            textBox2.Text = print.banbenhao;
            textBox3.Text = print.AIdaima;
            textBox4.Text = print.chanxianbanci;
        }
    }
}
